﻿Public Class FormMain
    Dim objUser As New clsUser
    Dim objPWck As New clsPWck
    Dim objGjbMst As New clsGjbMst
    Dim objFlbMst As New clsFlbMst
    Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        dgv.Font = New Font("UTF-8", 14)
        connectDB()
        getUserListByCJ()
        setCmb()
        setDgvData()
    End Sub

    Private Sub connectDB()
        g_szInitFileName = My.Application.Info.DirectoryPath & "\Client_Warehouse_PW.ini"

        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If

    End Sub

    Private Sub getUserListByCJ()
        Dim dtUser As DataTable = objUser.getUserListByCJ(1)
        cmbOper.DisplayMember = "user_name"
        cmbOper.ValueMember = "user_id"
        cmbOper.DataSource = dtUser
        If String.IsNullOrEmpty(g_Current_UserID) = False Then
            cmbOper.SelectedValue = g_Current_UserID
        End If
        cmbOper.Enabled = False
    End Sub

    Public Sub setCmb()
        Dim dtable As DataTable = New DataTable()
        dtable.Columns.Add("name")
        dtable.Columns.Add("value")
        dtable.Rows.Add("全部", "-1")
        dtable.Rows.Add("骨架板", "0")
        dtable.Rows.Add("法兰板", "1")
        cmbType.DisplayMember = "name"
        cmbType.ValueMember = "value"
        cmbType.DataSource = dtable
        cmbType.SelectedIndex = 0
    End Sub

    Public Sub setDgvData()
        Dim dt As DataTable = objPWck.getAllList(cmbType.SelectedValue)
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dt

        Dim dtPwCk As DataTable = objPWck.getAllPwCkList(cmbType.SelectedValue)
        dtPwCk.Columns.Add("oper_type_name")
        dtPwCk.Columns.Add("size")
        If dtPwCk.Rows.Count > 0 Then
            For Each row As DataRow In dtPwCk.Rows
                If row("oper_type") = "0" Then
                    row("oper_type_name") = "骨架板"
                    If DBNull.Value.Equals(row("material_code")) = False Then
                        Dim dtgjbmst As DataTable = objGjbMst.selectAll(row("material_code"))
                        row("size") = dtgjbmst.Rows(0)("size")
                    End If
                Else
                    row("oper_type_name") = "法兰板"
                    If DBNull.Value.Equals(row("material_code")) = False Then
                        Dim dtflbmst As DataTable = objFlbMst.selectAll(row("material_code"))
                        row("size") = dtflbmst.Rows(0)("size")
                    End If
                End If
            Next
        End If
        dgv1.AutoGenerateColumns = False
        dgv1.DataSource = dtPwCk
    End Sub

    Private Sub btnSearce_Click(sender As Object, e As EventArgs) Handles btnSearce.Click
        setDgvData()
    End Sub

    Private Sub btnInto_Click(sender As Object, e As EventArgs) Handles btnInto.Click
        Dim frmInto As New FormInto()
        frmInto.usr = cmbOper.Text.Trim()
        frmInto.ShowDialog(Me)
    End Sub

    Private Sub btnInto2_Click(sender As Object, e As EventArgs) Handles btnInto2.Click
        Dim frmInto2 As New FormInto2()
        frmInto2.ShowDialog(Me)
    End Sub

    Private Sub btnOut_Click(sender As Object, e As EventArgs)
        Dim frmOut As New FormOutRecord()
        frmOut.ShowDialog(Me)
    End Sub

    Private Sub btnRkRecord_Click(sender As Object, e As EventArgs) Handles btnRkRecord.Click
        Dim pwRkRecord As New FormIntoRecord()
        pwRkRecord.ShowDialog(Me)
    End Sub

    Private Sub btnDeliverRecord_Click(sender As Object, e As EventArgs) Handles btnDeliverRecord.Click
        Dim pwDeliverRecord As New FormOutRecord()
        pwDeliverRecord.ShowDialog(Me)
    End Sub

    Private Sub btnMesPlan_Click(sender As Object, e As EventArgs) Handles btnMesPlan.Click
        Dim formMesPlan As New FormMesPlan()
        formMesPlan.usr = cmbOper.Text.Trim()
        formMesPlan.ShowDialog(Me)
    End Sub

    Private Sub FormMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        DBacs.Disconnect()
    End Sub
End Class

